package yl.hs.bmipfull.service.report.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.material.IPurchaseReceiptDao;
import yl.hs.bmipfull.daos.production.IProductShipOrderDao;
import yl.hs.bmipfull.pojo.material.PurchaseReceiptEntity;
import yl.hs.bmipfull.pojo.material.SupplierEntity;
import yl.hs.bmipfull.pojo.production.ProductShipOrderEntity;
import yl.hs.bmipfull.service.report.IMonthlyReportService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.report.MonthlyRawReportVO;
import yl.hs.bmipfull.viewmodels.report.MonthlySalesReportVO;
import yl.hs.bmipfull.viewmodels.report.RawCostAnalysisVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @apiNote
 * @author 谢东
 * @since ${DATE} ${TIME} 创建
 */
@Service
public class MonthlyReportServiceImpl implements IMonthlyReportService {

    @Autowired
    private IProductShipOrderDao productShipOrderDao;
    @Autowired
    private IPurchaseReceiptDao purchaseReceiptDao;



    @Override
    public PageResult<MonthlySalesReportVO> getProductSalesMonthlyReport(PageQuery pageQuery, String productCateSn, int rowNum, int page) {
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<MonthlySalesReportVO> result = new PageResult(rowNum);
        result.setRecords(productShipOrderDao.getMonthlySalesReportCount(pageQuery.getQuery(),productCateSn,null,(List<Integer>)pageQuery.getQuery().get("states")));
        result.setPage(page);
        result.setData(productShipOrderDao.getMonthlySalesReport(pageQuery.getQuery(),productCateSn,null,(List<Integer>)pageQuery.getQuery().get("states")));
        return result;
    }

    @Override
    public PageResult<MonthlyRawReportVO> getMonthlyRawReportPageQuery(PageQuery pageQuery, int rowNum, int page, String productSn) {
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<MonthlyRawReportVO> result = new PageResult(rowNum);
        result.setData(purchaseReceiptDao.getMonthlyRawReport(pageQuery.getQuery(),productSn,null,(List<Integer>)pageQuery.getQuery().get("states")));
        result.setPage(page);
        result.setRecords(purchaseReceiptDao.getMonthlyRawReportCount(pageQuery.getQuery(),productSn,null,(List<Integer>)pageQuery.getQuery().get("states")));
        return result;
    }

    @Override
    public List<Object> getRawCostAnalysis(PageQuery pageQuery, String productSn) {
        List<Object> result=new ArrayList<>();
        List<PurchaseReceiptEntity> supplierList=purchaseReceiptDao.querySupplierGroupByRmCateSN(pageQuery.getQuery(),productSn,null,(List<Integer>)pageQuery.getQuery().get("states"));
        for (var entity: supplierList) {
            Map<String,Object> map=new HashMap<>();
            pageQuery.getQuery().put("supplierSN",entity.getSupplierSN());
            List<RawCostAnalysisVO> list=purchaseReceiptDao.getRawCostAnalysis(pageQuery.getQuery(),productSn,null,(List<Integer>)pageQuery.getQuery().get("states"));
            map.put("supplierSN",entity.getSupplierSN());
            map.put("supplierName",entity.getSupplierName());
            map.put("reportList",list);
            result.add(map);
        }
        return result;
    }
}
